from rest_framework.views import APIView
from api.serializers.huaweicloud import CloudSecurityUpdateSerializer, CloudSecurityCreateSerializer
from api.extension.auth import TokenAuthentication
from rest_framework.response import Response
from api.extension import return_code
from api.extension.huaweicloud_security import HuaWeiCloudMiddleware
from api.models import HuaWeiCloud, CloudSecurityGroups
from api.extension.throttle import CloudCreateThrottle


class HuaWeiCloudSecurityMiddleware(APIView):
    permission_classes = []
    authentication_classes = [TokenAuthentication, ]
    throttle_objects = [CloudCreateThrottle(), ]

    def post(self, request):
        serializer = CloudSecurityCreateSerializer(data=request.data)
        if not serializer.is_valid():
            return Response({"code": return_code.VALIDATE_ERROR, "error": serializer.errors})
        description = serializer.validated_data.get('description')
        remote_ip_prefix = serializer.validated_data.get('remote_ip_prefix')
        cloud_env = serializer.validated_data.get('cloud_env')
        cloud_object = CloudSecurityGroups.objects.create(description=description, remote_ip_prefix=remote_ip_prefix,
                                                          cloud_env=cloud_env)
        security_object = HuaWeiCloud.objects.filter(id=cloud_env).first()
        ak = security_object.cloud_ak
        sk = security_object.cloud_sk
        region = security_object.cloud_zone
        # 调用华为工具 ， 更新安全组规则
        security_obj = HuaWeiCloudMiddleware(ak, sk, region)
        security_group_id, client = security_obj.list_security_group_id()
        security_obj.create_security_group_rule(remote_ip_prefix, description, security_group_id, client)
        cloud_object.save()
        for throttle in self.get_throttles():
            throttle.done()
        return Response({"code": return_code.SUCCESS, "data": "新增安全组IP完成"})

    def put(self, request):
        serializer = CloudSecurityUpdateSerializer(data=request.data)
        if not serializer.is_valid():
            return Response({"code": return_code.VALIDATE_ERROR, "detail": serializer.errors})
        # 通过用户数据 拿出对应的数据
        cloud_env = serializer.validated_data.get('cloud_env')
        description = serializer.validated_data.get('description')
        remote_ip_prefix = serializer.validated_data.get('remote_ip_prefix')
        #  查找数据库，拿到存放的 ak sk region
        cloud_object = HuaWeiCloud.objects.filter(id=cloud_env).first()
        security_object = CloudSecurityGroups.objects.filter(description=description).first()
        ak = cloud_object.cloud_ak
        sk = cloud_object.cloud_sk
        region = cloud_object.cloud_zone
        # 调用华为工具 ， 更新安全组规则
        security_obj = HuaWeiCloudMiddleware(ak, sk, region)
        security_id, security_group_id, client = security_obj.list_security_group_rules_id(description, request.method)
        security_obj.delete_security(security_id=security_id, client=client)
        security_obj.create_security_group_rule(remote_ip_prefix, description, security_group_id, client)
        # 把更新的结果结果入库
        security_object.remote_ip_prefix = remote_ip_prefix
        security_object.save()
        for throttle in self.get_throttles():
            throttle.done()
        return Response({"code": return_code.SUCCESS, "data": "更新安全组IP完成"})
